/**
例1：
输入: nums = [-1,0,3,5,9,12], target = 9     
输出: 4       
解释: 9 出现在 nums 中并且下标为 4  

例2：
输入: nums = [-1,0,3,5,9,12], target = 2     
输出: -1        
解释: 2 不存在 nums 中因此返回 -1  

 */

function find2(list, target) {
  let left = 0;
  let right = list.length - 1;
  while (left <= right) {
    const middle = left + Math.floor((right - left) / 2);
    if (target < list[middle]) {
      right = middle - 1;
    } else if (target > list[middle]) {
      left = middle + 1;
    } else {
      return middle;
    }
  }
  return -1;
}
console.log("示例1:", find2([-1, 0, 3, 5, 9, 12], 9));
console.log("示例2:", find2([-1, 0, 3, 5, 9, 12], 2));
